<?php
namespace app\admin\controller;
use app\admin\logic\ActivityAreaLogic;
class ActivityController extends Controller
{
    public function index(){
        $this->assign('activity_list',ActivityAreaLogic::getActivityList());
        return $this->fetch();
    }
    /**
     * note 活动专区列表
     */
    public function areaLists(){
        if($this->request->isAjax()){
            $get = $this->request->get();
            $list = ActivityAreaLogic::areaLists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list
            ]);
        }
    }

    /**
     * note 活动商品列表
     */
    public function goodsLists(){
        if($this->request->isAjax()){
            $get = $this->request->get();
            $list = ActivityAreaLogic::goodsLists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list
            ]);
        }
    }

    /**
     * note 添加活动专区
     */
    public function addActivity(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $result = $this->validate($post,'app\admin\validate\ActivityArea.add');
            if($result === true){
                ActivityAreaLogic::addActivity($post);
               return success('新增成功');
            }
            return error($result);
        }
        return $this->fetch();
    }

    /**
     * note 编辑活动专区
     */
    public function editActivity($id){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['delete_time '] = 0;
            $result = $this->validate($post,'app\admin\validate\ActivityArea');
            if($result === true){
                ActivityAreaLogic::editActivity($post);
                return success('编辑成功');
            }
            return error($result);
        }
        $this->assign('info',ActivityAreaLogic::getActivity($id));
        return $this->fetch();
    }

    /**
     * note 删除活动专区
     */
    public function delActivity(){
        $id = $this->request->post('id');
        ActivityAreaLogic::delActivity($id);
        return success('删除成功');

    }

    /**
     * note 添加活动商品
     */
    public function addGoods(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['goods_list'] = form_to_linear($post);
            $result = $this->validate($post,'app\admin\validate\ActivityGoods.add');
            if($result === true){
                ActivityAreaLogic::addGoods($post);
                return success('新增成功');
            }
            return error($result);
        }
        $this->assign('activity_list',ActivityAreaLogic::getActivityList());
        return $this->fetch();
    }

    /**
     * note 编辑活动商品
     */
    public function editGoods(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['delete_time '] = 0;
            $result = $this->validate($post,'app\admin\validate\ActivityGoods');

            if($result === true){
                ActivityAreaLogic::editGoods($post);
                return success('新增成功');
            }
            return error($result);
        }
        $goods_id = $this->request->get('goods_id');
        $activity_id = $this->request->get('activity_id');
        $this->assign('activity_list',ActivityAreaLogic::getActivityList());
        $this->assign('info',ActivityAreaLogic::getActivityGoods($goods_id,$activity_id));
        return $this->fetch();
    }

    /**
     * note 删除活动商品
     */
    public function delGoods(){
        $goods_id = $this->request->post('goods_id');
        $activity_id = $this->request->post('activity_id');
        $result = ActivityAreaLogic::delGoods($goods_id,$activity_id);
        if($result == true){
            return success('删除成功');
        }
        return error('删除失败');
    }

}